mui.init({
	beforeback: function () {
		//获得列表界面的webview
        var parentOobj = plus.webview.currentWebview().opener();
        var memObj = plus.webview.getWebviewById("src/templates/tab/member.html");
		var boughtObj = plus.webview.getWebviewById("src/templates/tab/bought.html");
		mui.fire(parentOobj, 'dataList');
        mui.fire(memObj, 'dataList');
		mui.fire(boughtObj, 'updateData');
		//返回true，继续页面关闭逻辑
		return true;
	}
});

mui.plusReady(function () {
	plus.navigator.setFullscreen(false);
	plus.oauth.getServices(function (services) {
		auths = services;
	}, function (e) {
		alert("获取登录服务列表失败：" + e.message + " - " + e.code);
	});
	//新用户注册跳转
	document.querySelector('.register').addEventListener('tap', function () {
		mui.openWindow({
			url: 'register.html',
			id: 'registerHtml',
		})
	});

	//忘记密码跳转
	document.querySelector('.forgetPwd').addEventListener('tap', function () {
		mui.openWindow({
			url: 'forgetPwd.html'
		})
	});

	//点击登录
	document.querySelector('.login_btn').addEventListener('tap', function () {
        showWatting();
		var api_host = plus.storage.getItem('HOST_URL');
		mui.ajax({
			url: api_host + '/api/linxuan/user/login',
			data: $('#login_form').serialize(),
			dataType: 'json',
			type: 'post',
			headers:{
				'User-Token':plus.storage.getItem('UserToken'),
				'uuid':plus.device.uuid, //设备的唯一标识
			},
			timeout: 3000,
			success: function (msg) {
				if (msg.code == 1) {
					mui.toast(msg.msg, {duration: '1500',type: 'div'});
					//存入新的token信息
					plus.storage.setItem('UserToken', msg.data);
					//跳转到首页
					setTimeout(function () {
						mui.back();
					}, 1500)
				} else {
					mui.toast(msg.msg, {duration: '1000',type: 'div'});
				}
			},
			error: function (xhr, type, errorThrown) {
				mui.toast('请求失败，请检查网络！', {duration: '2000',type: 'div'});
			},
			complete:function () {
                plus.nativeUI.closeWaiting();
            }
		});
	})
});


//显示隐藏密码(原生js与jq可以共存，但不能混用)
var pwdEyes = document.getElementById('pwdEyes');
var showPwd = $("#password");

pwdEyes.addEventListener('tap', function () {
	if ($(this).hasClass('eyeOn')) {
		$(this).removeClass('eyeOn').addClass('eyeOff');
		showPwd.prop('type', 'text');
	} else {
		$(this).removeClass('eyeOff').addClass('eyeOn');
		showPwd.prop('type', 'password');
	}
});

document.getElementById('weixin').addEventListener('tap', function () {
	authLogin('weixin');
})
document.getElementById('qq').addEventListener('tap', function () {
	authLogin('qq');
})

// 登录操作
function authLogin(type) {
	var s;
	for (var i = 0; i < auths.length; i++) {
		if (auths[i].id == type) {
			s = auths[i];
			break;
		}
	}
	if (!s.authResult) {
		s.login(function (e) {
			mui.toast("登录认证成功！");
			authUserInfo(type);
		}, function (e) {
			mui.toast("登录认证失败！");
		});
	} else {
		mui.toast("已经登录认证！");
		authUserInfo(type);
	}
}
//注销
function authLogout() {
	for (var i in auths) {
		var s = auths[i];
		if (s.authResult) {
			s.logout(function (e) {
				console.log("注销登录认证成功");
			}, function (e) {
				console.log("注销登录认证失败");
			});
		}
	}
}
// 三方登录认证信息
function authUserInfo(type) {
	var s;
	for (var i = 0; i < auths.length; i++) {
		if (auths[i].id == type) {
			s = auths[i];
			break;
		}
	}
	if (!s.authResult) {
		mui.toast("未授权登录！");
	} else {
		s.getUserInfo(function (e) {
			//var userStr = JSON.stringify(s.userInfo); //用户信息,openid,nickname,sex,language,city,province,country,headimgurl
			var loginStr = JSON.stringify(s.authResult); //登录信息,需要access_token,openid,unionid
			var api_host = plus.storage.getItem('HOST_URL');
			//微信通过unionid查询用户信息
			//QQ通过openid查询用户信息
			mui.ajax({
				url: api_host+'/api/linxuan/user/synlogin',
				dataType: 'json',
				data: {
					'type': type,
					//'userinfo': userStr,
					'logininfo': loginStr,
				},
				type: 'POST',
				headers:{'User-Token':plus.storage.getItem('UserToken')},
				timeout:3000,
				success: function (msg) {
					if (msg.code == 1) { //登录成功
						if(msg.msg=='跳转到绑定账号页面'){ //绑定账号
							plus.storage.setItem('synlogintype', type);
							plus.storage.setItem('synlogininfo', loginStr);
							mui.openWindow({
								url: 'bindAccount.html',
								id: 'bindAccount'
							})
						}else{
							plus.storage.setItem('UserToken', msg.data);
							mui.toast(msg.msg, {
								duration: '2000'
							});
							setTimeout(function () {
								mui.back();
							}, 1000)
						}
					}else { //登录失败
						mui.toast("登录账号失败！");
					}
				},
				error: function (xhr, type, errorThrown) {
					mui.toast("请求失败，请检查网络！");
				}
			})
			authLogout();
		}, function (e) {
			alert("获取用户信息失败：" + e.message + " - " + e.code);
		});
	}
}

//解决文本框获取焦点时,定位元素被软键盘顶上去
var win_h = $(window).height();
window.addEventListener('resize', function () {
    if($(window).height() < win_h){
        $('.otherLogin').hide();
    }else{
        $('.otherLogin').show();
    }
});